#include <asm/asm.h>
#include <macros.h>

restore_all:
	popq	%rax
	movq	%rax,	%es

	popq	%rax
	movq	%rax,	%ds

	popq	%r15
	popq	%r14
	popq	%r13
	popq	%r12
	popq	%r11
	popq	%r10
	popq	%r9
	popq	%r8
	popq	%rbp
	popq	%rdi
	popq	%rsi
	popq	%rdx
	popq	%rcx
	popq	%rbx
	popq	%rax
	addq    $0x10, %rsp #跳过错误码和函数指针
	iretq

isr_call_comm:
# 以下所有中断异常步骤都是一致的
	cld
	pushq   %rax
	pushq	%rbx
	pushq	%rcx
	pushq	%rdx
	pushq	%rsi
	pushq	%rdi
	pushq	%rbp
	pushq	%r8
	pushq	%r9
	pushq	%r10
	pushq	%r11
	pushq	%r12
	pushq	%r13
	pushq	%r14
	pushq	%r15
	xorq    %rax, %rax
	movq	%ds,	%rax
	pushq	%rax
	xorq    %rax, %rax
	movq	%es,	%rax
	pushq	%rax
	movq $SYS_DATA64_IDX, %rdi
	movq %rdi, %es
	movq %rdi, %ds

# x86-64 参数传递从左到右依次 rdi rsi rdx rcx r8 r9
	movq %rsp, %rdi #将intr_arg_t指针作为函数参数
	movq GET_FUNC_POINT(%rsp), %rax #获取函数指针
    callq *%rax

	jmp restore_all



BEGIN_FUNC(keyboadr_isr)
    pushq $0 #错误码的位置
	cld
	pushq $0 #函数指针的位置
	pushq   %rax
	pushq	%rbx
	pushq	%rcx
	pushq	%rdx
	pushq	%rsi
	pushq	%rdi
	pushq	%rbp
	pushq	%r8
	pushq	%r9
	pushq	%r10
	pushq	%r11
	pushq	%r12
	pushq	%r13
	pushq	%r14
	pushq	%r15
	xorq    %rax,   %rax
	movq	%ds,	%rax
	pushq	%rax
	xorq    %rax,   %rax
	movq	%es,	%rax
	pushq	%rax

	movq	$SYS_DATA64_IDX,	%rax
	movq	%rax,	%ds
	movq	%rax,	%es

	movq %rsp, %rdi #将intr_arg_t指针作为函数参数
	movq $1, %rsi # 此时为中断号
    callq do_keyboard_isr


    jmp restore_all

END_FUNC(keyboadr_isr)


